import getDataForDB from '@/api/qsbserviceEnterprise'


const getBusinessType = async()=>{
  const res = {optionList:[],tableTypeKeyName:{},tableTypeKeyValue:{}}
  // { label: '税务登记', value: '1013' },
  // 1001: '注册服务费',
  const { code, result } = await getDataForDB('获取所有BusinessType数据')
  if(code==200){
    // {
		// 	"name": "注册服务费",
		// 	"value": 1001
		// }
    result.forEach(item=>{
      const {name,value} = item
      res.optionList.push({label: name, value: value })
      res.tableTypeKeyName[value] = name
      res.tableTypeKeyValue[name] = value
    })
    // pageSelf.QueryConfig.formListConfig.formList[2].optionList = res.optionList
  }
  // console.log('getBusinessType===>',pageSelf.QueryConfig.formListConfig.formList[2],res,result);
  return res
}

const getData = async (param) => {
  // const param = {}
  let res = {}
  try {
    const { code, result } = await getDataForDB('订单列表', param)
    if (code == 200) {
      res = result
    }
  } catch (error) {
    console.log('订单列表 接口报错，具体原因如下：', error);
  }
  console.log('订单列表===>', res,param);
  return res
}

/**
 * 请求参数处理
 * @param {*} data_
 * @returns
 */
 const ParameterProcessing = (data_,Mparam={})=>{

  const { data ,pageNo,pageSize} = data_
  const tempData = {}
  Object.keys(data).forEach(key=>{
    const tenpVal = data[key]
    if(Object.prototype.toString.call(tenpVal) === '[object String]'&& tenpVal !=''){
      tempData[key] = tenpVal
    }
    if(Object.prototype.toString.call(tenpVal) === '[object Number]'){
      tempData[key] = tenpVal
    }
    if(Object.prototype.toString.call(tenpVal) === '[object Array]' && tenpVal.length>0){
      tempData[key] = tenpVal
    }

    // console.log(key,tenpVal,tenpVal.length,tempData);
  })
  const ll = { data:{...tempData,...Mparam} ,pageNo,pageSize}
  console.log('ParameterProcessing===>',data_,ll);
  return ll
}
// "data": {
//   "businessStatus": 0,
//   "businessType": 1001,
//   "channelName": "xxx渠道",
//   "createTimeEnd": "2022-10-28",
//   "createTimeStart": "2022-10-27",
//   "entName": "xxx渠道",
//   "payerType": 0
// },
// "pageNo": 0,
// "pageSize": 0
const query = async (pageSelf, index, data_) => {
  console.log('查询事件====>  ', index, data_);
  const { data, ...args } = data_
  const tempData = { data: { ...data_.data, isInvoicing: data && data.isInvoicing   , businessStatus: tabList[index] }, ...args }
  window['ServiceEnterpriseList_param'] = tempData
  const res = await getData(ParameterProcessing(tempData))
  // console.log('==============================>',res,res.result);
  // if(res.success){
    const {orderInfoListVoPage,  totalNum, toBeReviewedNum, toBeCompleteNum, toBePayNum, processingNum, finishNum} = res
    const { total, records, size, current } = orderInfoListVoPage

    pageSelf['tableList'][index].dataSource = records
    pageSelf['tableList'][index].total = total
    pageSelf['tableList'][index].current = current
    pageSelf['tableList'][index].pageSize = size

    pageSelf['tableList'][0].tabName = `全部（${totalNum || 0}）`
    pageSelf['tableList'][1].tabName = `待审核（${toBeReviewedNum || 0}）`
    pageSelf['tableList'][2].tabName = `待完善（${toBeCompleteNum || 0}）`
    pageSelf['tableList'][3].tabName = `待支付（${toBePayNum || 0}）`
    pageSelf['tableList'][4].tabName = `处理中（${processingNum || 0}）`
    pageSelf['tableList'][5].tabName = `处理完成（${finishNum || 0}）`
  // }
  // if (res.records) {
  //   const { total, records, size, current } = res
  //   pageSelf['tableList'][index].dataSource = records
  //   pageSelf['tableList'][index].total = total
  //   pageSelf['tableList'][index].current = current
  //   pageSelf['tableList'][index].pageSize = size
  //   const { totalNum, toBeReviewedNum, toBeCompleteNum, toBePayNum, processingNum, finishNum } = records[0] || {}
  //   pageSelf['tableList'][0].tabName = `全部（${totalNum || 0}）`
  //   pageSelf['tableList'][1].tabName = `待审核（${toBeReviewedNum || 0}）`
  //   pageSelf['tableList'][2].tabName = `待完善（${toBeCompleteNum || 0}）`
  //   pageSelf['tableList'][3].tabName = `待支付（${toBePayNum || 0}）`
  //   pageSelf['tableList'][4].tabName = `处理中（${processingNum || 0}）`
  //   pageSelf['tableList'][5].tabName = `处理完成（${finishNum || 0}）`
  // }
  // else {
  //   // 当借口正常之后删除此代码
  //   pageSelf['tableList'][index].total = 100
  //   const tempArr = [
  //     { id: '1', entName: '企业名称1', entType: '0', businessType: '1001', businessStatus: '0', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '4', entName: '企业名称4', entType: '1', businessType: '1002', businessStatus: '1', createTime: '', isInvoicing: '0', flowCode: '1', flowNo: '2' },
  //     { id: '2', entName: '企业名称2', entType: '2', businessType: '1003', businessStatus: '2', createTime: '', isInvoicing: '2', flowCode: '1', flowNo: '2' },
  //     { id: '3', entName: '企业名称3', entType: '3', businessType: '1004', businessStatus: '3', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '5', entName: '企业名称3', entType: '4', businessType: '1005', businessStatus: '4', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '6', entName: '企业名称3', entType: '0', businessType: '1006', businessStatus: '5', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '7', entName: '企业名称3', entType: '1', businessType: '1007', businessStatus: '6', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '8', entName: '企业名称3', entType: '2', businessType: '1008', businessStatus: '7', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '9', entName: '企业名称3', entType: '3', businessType: '1009', businessStatus: '8', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //     { id: '3', entName: '企业名称3', entType: '4', businessType: '1013', businessStatus: '8', createTime: '', isInvoicing: '1', flowCode: '1', flowNo: '2' },
  //   ]
  //   pageSelf['tableList'][index].dataSource = tempArr
  //   pageSelf['tableList'][index].current = data_.pageNo
  // }

}

// 0-待完善;1-待审核;2-审核驳回;3-待支付;4-办理中;5-已完成;6-已开票;7-已取消;8-支付失败
const tabList = {
  0: '',
  1: '1',
  2: '0',
  3: '3',
  4: '4',
  5: '5',
}
const tabChange = (pageSelf, index, data) => {

  query(pageSelf, index, data)
}

/**
 * 根据企业id查询企业的当前状态
 * @param {*} entId
 * @returns
 */
const getDataForEntState = async (pageSelf, entId) => {
  // const param = {}
  console.log('根据企业id查询企业的当前状态===>', entId);
  let res = 0
  try {
    const { code, result } = await getDataForDB('根据企业id查询企业的当前状态', { entId })
    if (code == 200) {
      res = result
    }
  } catch (error) {
    console.log('根据企业id查询企业的当前状态 接口报错，具体原因如下：', error);
  }
  console.log('订单列表===>', res);
  return res
}

const pageEvent = {
  query,
  tabChange,
  getDataForEntState,
  getBusinessType
}

export default (type, pageSelf, ...args) => {
  return pageEvent[type](pageSelf, ...args)
}
